$(function() {
    //1.请求当前登录用户信息
    getUserinfo()
        //为退出按钮绑定事件方法
    $('#btnLogout').on('click', logout)

});
//2.封装- 请求当前登录用户信息接口
function getUserinfo() {
    $.ajax({
        method: 'get',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // },
        success(res) {
            layer.msg(res.message)
            if (res.status !== 0) {
                return
            }
            render(res.data);
        }
    })
};

//3.封装-根据用户数据 渲染头像和名称
function render(usrInfo) {
    const usrName = usrInfo.nickname || usrInfo.username;
    $('#usrName').text(usrName);
    if (usrInfo.user_pic) {
        $('.img-avatar').attr('src', usrInfo.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.text-avatar').text(usrName[0].toUpperCase()).show()
        $('.img-avatar').hide()
    }
};

//封装-退出
function logout(e) {
    e.preventDefault();
    layui.layer.confirm('你确定要退出吗?', { icon: 3, title: '提示' }, function(index) {
        //删除token
        localStorage.removeItem('token')
            //跳转 登录页面
        location.href = '/login.html'
    });
}